package com.tsinghuabigdata.edu.ddmath.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.adapter.FiltrateAdapter;
import com.tsinghuabigdata.edu.ddmath.util.DensityUtils;

import java.util.List;


public class SpinnerView extends RelativeLayout implements OnClickListener {

    private String Tag = "SpinnerView";
    private Context             mContext;
    private CheckBox            mCbFiltrate;
    private FiltrateAdapter     mAdapter;
    private OnItemClickListener mOnItemClickListener;
    //    private PopupWindow         mWindow;
    private Dialog              mDialog;
    private LinearLayout        mLlFiltrate;
    private boolean             checked;
    HanddleSpinnerView mHanddleSpinnerView;


    public int getSelectedPosition() {
        return selectedPosition;
    }

    public void setSelectedPosition(int selectedPosition) {
        this.selectedPosition = selectedPosition;
        if (mAdapter != null)
            mAdapter.setSelectPostion(selectedPosition);
    }

    private int selectedPosition;


    public SpinnerView(Context context) {
        this(context, null);
    }

    public SpinnerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(context, R.layout.v_spinner, this);
        mContext = context;
        initView();
        initEvent();
    }

    public void setHanddleSpinnerView(HanddleSpinnerView handdleSpinnerView) {
        mHanddleSpinnerView = handdleSpinnerView;
    }

    private void initEvent() {
        mLlFiltrate.setOnClickListener(this);
    }

    private void initView() {
        mLlFiltrate = (LinearLayout) findViewById(R.id.ll_filtrate);
        mCbFiltrate = (CheckBox) findViewById(R.id.cb_filtrate);
    }

    @Override
    public void onClick(View v) {
        if (v == mLlFiltrate) {
            Log.i(Tag, "mLlFiltrate   onClick()");
            if (checked) {
                return;
            } else {
                mHanddleSpinnerView.select();
                showPopupWindow();
            }
        }
    }

    private void showPopupWindow() {
        Log.i(Tag, "showPopupWindow()");
        setChecked(true);
        if (mDialog != null) {
            mDialog.show();
            mAdapter.notifyDataSetChanged();
            return;
        }
        mDialog = new Dialog(getContext(), R.style.dialog);
        //        mDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        View root = View.inflate(mContext, R.layout.dialog_course, null);
        mDialog.setContentView(root);
        ListView listView = (ListView) root.findViewById(R.id.lv_spinner);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(mOnItemClickListener);

        Window dialogWindow = mDialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.TOP);
        int left = DensityUtils.dp2px(getContext(), 131 + 30) + getLeft();
        int top = DensityUtils.dp2px(getContext(), 31 + 110 + 15 + 25 + 10);
        //        int top = DensityUtils.dp2px(getContext(), 31 + 110)+getBottom();
        lp.x = left;
        lp.y = top;
        Log.i(Tag, "left=" + left + " top=" + top);
        int w = DensityUtils.dp2px(mContext, 130);
        lp.width = w;
        lp.dimAmount = 0.1f;
        dialogWindow.setAttributes(lp);
        //        dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        mDialog.setCancelable(false);
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                setChecked(false);
            }
        });

        mDialog.show();

    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean isChecked) {
        this.checked = isChecked;
        mCbFiltrate.setChecked(isChecked);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void setText(CharSequence text) {
        mCbFiltrate.setText(text);
    }

    public String getText() {
        return mCbFiltrate.getText().toString();
    }

    public void dismiss() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    public void setData(List<String> listPeriod) {
        mAdapter = new FiltrateAdapter(mContext, listPeriod, selectedPosition);
    }

    public interface HanddleSpinnerView {
        void select();
    }
}
